import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
 
public class Server {
 
    public static void main(String[] args) {
     
        try {
            ServerSocket serverSocket = new ServerSocket(12345);
            System.out.println("server listen is begin");
            Socket socket = serverSocket.accept();
            InputStream is = socket.getInputStream();
            InputStreamReader reader = new InputStreamReader(is);
            BufferedReader bufReader = new BufferedReader(reader);
            String s = null;
            StringBuffer sb = new StringBuffer();
            while ((s = bufReader.readLine()) != null) {
                sb.append(s);
            }
            System.out.println("server:" + sb.toString());
            socket.shutdownInput();
 
            OutputStream os = socket.getOutputStream();
            os.write(("I am server,I accept data is "+sb.toString()).getBytes());
            os.flush();
            socket.shutdownOutput();
            os.close();
            bufReader.close();
            reader.close();
            is.close();
 
            socket.close();
            serverSocket.close();
 
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
